package com.example.visuallearning4toddlers;

import java.io.InputStream;
import java.util.Locale;
import java.util.Random;

import com.example.visuallearning4toddlers.util.SystemUiHider;

import android.R.string;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class Age_group_Screen extends Activity /*implements TextToSpeech.OnInitListener*/ {
	
	//private TextToSpeech tts;
	private TextView txtText = (TextView)findViewById(R.id.getText);
	//private TextView showText = (TextView)findViewById(R.id.showText);
	
	/**
	 * Whether or not the system UI should be auto-hidden after
	 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
	 */
	private static final boolean AUTO_HIDE = true;

	/**
	 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
	 * user interaction before hiding the system UI.
	 */
	private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

	/**
	 * If set, will toggle the system UI visibility upon interaction. Otherwise,
	 * will show the system UI visibility upon interaction.
	 */
	private static final boolean TOGGLE_ON_CLICK = true;

	/**
	 * The flags to pass to {@link SystemUiHider#getInstance}.
	 */
	private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

	/**
	 * The instance of the {@link SystemUiHider} for this activity.
	 */
	private SystemUiHider mSystemUiHider;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_age_group__screen);
		
		//tts = new TextToSpeech(this, this);

		final View controlsView = findViewById(R.id.fullscreen_content_controls);
		final View contentView = findViewById(R.id.fullscreen_content);

		// Set up an instance of SystemUiHider to control the system UI for
		// this activity.
		mSystemUiHider = SystemUiHider.getInstance(this, contentView,
				HIDER_FLAGS);
		mSystemUiHider.setup();
		mSystemUiHider
				.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
					// Cached values.
					int mControlsHeight;
					int mShortAnimTime;

					@Override
					@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
					public void onVisibilityChange(boolean visible) {
						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
							// If the ViewPropertyAnimator API is available
							// (Honeycomb MR2 and later), use it to animate the
							// in-layout UI controls at the bottom of the
							// screen.
							if (mControlsHeight == 0) {
								mControlsHeight = controlsView.getHeight();
							}
							if (mShortAnimTime == 0) {
								mShortAnimTime = getResources().getInteger(
										android.R.integer.config_shortAnimTime);
							}
							controlsView
									.animate()
									.translationY(visible ? 0 : mControlsHeight)
									.setDuration(mShortAnimTime);
						} else {
							// If the ViewPropertyAnimator APIs aren't
							// available, simply show or hide the in-layout UI
							// controls.
							controlsView.setVisibility(visible ? View.VISIBLE
									: View.GONE);
						}

						if (visible && AUTO_HIDE) {
							// Schedule a hide().
							delayedHide(AUTO_HIDE_DELAY_MILLIS);
						}
					}
				});

		// Set up the user interaction to manually show or hide the system UI.
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (TOGGLE_ON_CLICK) {
					mSystemUiHider.toggle();
				} else {
					mSystemUiHider.show();
				}
			}
		});

		// Upon interacting with UI controls, delay any scheduled hide()
		// operations to prevent the jarring behavior of controls going away
		// while interacting with the UI.
		
	}

	public void initiateModeSelect(View view){
		setContentView(R.layout.mode_select_screen);
	}
	public void initiateLearning(View view){
		setContentView(R.layout.learning_screen);
		//tts = new TextToSpeech(this, this);
		
		/*ImageView button = (ImageView) findViewById(R.id.starButton);
		InputStream resource = getResources().openRawResource(R.drawable.colors);
		Bitmap bitmap = BitmapFactory.decodeStream(resource);
		button.setBackground(new StarBitmapDrawable(bitmap));*/
	}

	public void initiateKnowledge(View view){
		setContentView(R.layout.knowledge_screen);
		//tts = new TextToSpeech(this, this);
		//loadKScreen(view);
	}
	public void goHome(View view){
		setContentView(R.layout.activity_age_group__screen);		
	}
	public void goBack(View view){
		setContentView(R.layout.mode_select_screen);
	}
	
	public void actionButton(View view){
		
		Button clickedButton = (Button) view;
		TextView tv = (TextView)findViewById(R.id.getText);
	
		switch (clickedButton.getId()) {
			case R.id.imageButton4:
				tv.setText("This is a blue rectangle.");
			break;
			case R.id.imageButton3:
				tv.setText("This is a green oval.");
			break;
			case R.id.imageButton2:
				tv.setText("This is a red triangle.");
			break;
			case R.id.imageButton1:				
				tv.setText("This is a yellow square.");
			break;
			/*case R.id.starButton:
				tv.setText("This is an orange star.");
			break;*/
		}
	}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		// Trigger the initial hide() shortly after the activity has been
		// created, to briefly hint to the user that UI controls
		// are available.
		delayedHide(100);
	}

	/**
	 * Touch listener to use for in-layout UI controls to delay hiding the
	 * system UI. This is to prevent the jarring behavior of controls going away
	 * while interacting with activity UI.
	 */
	View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent motionEvent) {
			if (AUTO_HIDE) {
				delayedHide(AUTO_HIDE_DELAY_MILLIS);
			}
			return false;
		}
	};

	Handler mHideHandler = new Handler();
	Runnable mHideRunnable = new Runnable() {
		@Override
		public void run() {
			mSystemUiHider.hide();
		}
	};

	/**
	 * Schedules a call to hide() in [delay] milliseconds, canceling any
	 * previously scheduled calls.
	 */
	private void delayedHide(int delayMillis) {
		mHideHandler.removeCallbacks(mHideRunnable);
		mHideHandler.postDelayed(mHideRunnable, delayMillis);
	}
	
	//Method to check to see if tts is activated and to speak. 
	/*public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS){
			int result = tts.setLanguage(Locale.US);
			
			if (result == TextToSpeech.LANG_MISSING_DATA ||
					result == TextToSpeech.LANG_NOT_SUPPORTED){
				Log.e("TTS", "This Language is not supported");
			} else {			
					speakOut();
			}
					
		} else{
			Log.e("TTS", "Initialization Failed!");
		}
	}
		
	private void speakOut() {
		// Method to take the text and speak.
		
		String lText = txtText.getText().toString();
		String kText = showText.getText().toString();
		
		if(ContentView.equals(R.layout.learning_screen)){
			tts.speak(lText, TextToSpeech.QUEUE_FLUSH, null);
		}else{
			if(ContentView.equals(R.layout.knowledge_screen)){
			tts.speak(kText, TextToSpeech.QUEUE_FLUSH, null);
			}
		}
	}*/
	
	/*private void gjSpeakOut(){
		
		String gj = showText.getText().toString();
		tts.speak(gj, TextToSpeech.QUEUE_FLUSH, null);
	}
	
	private void taSpeakOut(){
		String ta = showText.getText().toString();
		tts.speak(ta, TextToSpeech.QUEUE_FLUSH, null);
	}*/
	
	/*@Override
	public void onDestroy(){
		//Shutdown tts
		if(tts != null) {
			tts.stop();
			tts.shutdown();
		}
		super.onDestroy();
	} */
	
	/*//Method to load the knowledge screen based on the age group selected
	private void loadKScreen(View view){
		
		Button clickedButton = (Button) view;
		boolean eighteenmos = clickedButton.equals(R.id.shapesncolorsButton);
		boolean twoyear = clickedButton.equals(R.id.shapesncolorsButton);
		boolean threeyear = clickedButton.equals(R.id.shapesncolorsButton);
		
		//Check to see which age group is picked.
		if(eighteenmos = true){
			loadTwoOptions(view);
		}else{
			if(twoyear = true){
				loadThreeOptions(view);
			}else{
				if(threeyear = true){
					loadFourOptions(view);
				}
			}
		}
	}

	//Method to load two shape and color options
	private void loadTwoOptions(View view) {
		
		Button image1 = (Button)findViewById(R.id.img1);
		Button image2 = (Button)findViewById(R.id.img2);
		
		//Loop for a 10 set of options.
		for(int i=0; i<10; i++){
			
			showText.setText(randomStrings());
			image1.setBackground(getImage());
			image2.setBackgroundResource(randomShape());
			
			//Text to show if the correct answer is chosen.
			if(image1.isSelected()){
				showText.setText("Good Job");
				//gjSpeakOut();
			}else{
				showText.setText("Try Again");
				//taSpeakOut();
			}			
		}			
	}


	//Method to load three shape and color options
	private void loadThreeOptions(View view) {
		
		Button image1 = (Button)findViewById(R.id.img1);
		Button image2 = (Button)findViewById(R.id.img2);
		Button image3 = (Button)findViewById(R.id.img3);
		
		//Loop for a 10 set of options.
		for(int i=0; i<10; i++){
			
			showText.setText(randomStrings());
			image1.setBackground(getImage());
			image2.setBackgroundResource(randomShape());
			image3.setBackgroundResource(randomShape());
			
			//Text to show if the correct answer is chosen.
			if(image1.isSelected()){
				showText.setText("Good Job");
				//gjSpeakOut();
			}else{
				showText.setText("Try Again");
				//taSpeakOut();
			}
		}			
	}
	
	//Method to load four shape and color options
	private void loadFourOptions(View view) {
		
		Button image1 = (Button)findViewById(R.id.img1);
		Button image2 = (Button)findViewById(R.id.img2);
		Button image3 = (Button)findViewById(R.id.img3);
		Button image4 = (Button)findViewById(R.id.img4);
		
		//Loop for a 10 set of options.
		for(int i=0; i<10; i++){
			
			showText.setText(randomStrings());
			image1.setBackground(getImage());
			image2.setBackgroundResource(randomShape());
			image3.setBackgroundResource(randomShape());
			image4.setBackgroundResource(randomShape());
			
			//Text to show if the correct answer is chosen.
			if(image1.isSelected()){
				showText.setText("Good Job");
				//gjSpeakOut();
			}else{
				showText.setText("Try Again");
				//taSpeakOut();
			}
		}	
	}

	//Random shape generator based on the drawables.
	private int randomShape(){
		final TypedArray images = getResources().obtainTypedArray(R.array.shape_array);
		final Random rand = new Random();
		final int rndInt = rand.nextInt(images.length());
		final int resID = images.getResourceId(rndInt, 0);
		return rndInt;
	}
	
	//Random string generator based on the array of strings. 
	private String randomStrings(){
		final String[] strings = getResources().getStringArray(R.array.string_array);
		String randomStr = strings[new Random().nextInt(strings.length)];
		return randomStr;
	}
	
	//Method to get the image and set it into the view based on text. 
	private Drawable getImage() {
		//Variable to hold the image to return
		Drawable image = null;
		
		if(showText.equals("Find the red square.")){
			image.equals(R.drawable.red_square_button);
		}else{
			if(showText.equals("Find the red circle.")){
				image.equals(R.drawable.red_oval_button);
		}else{
			if(showText.equals("Find the red rectangle.")){
				image.equals(R.drawable.red_rectangle_button);
		}else{
			if(showText.equals("Find the red triangle.")){
				image.equals(R.drawable.red_triangle_button);
		}else{
			if(showText.equals("Find the blue square.")){
				image.equals(R.drawable.blue_square_button);
		}else{
			if(showText.equals("Find the blue circle.")){
				image.equals(R.drawable.blue_oval_button);
		}else{
			if(showText.equals("Find the blue rectangle.")){
				image.equals(R.drawable.blue_rectangle_button);
		}else{
			if(showText.equals("Find the blue triangle.")){
				image.equals(R.drawable.blue_triangle_button);
		}else{
			if(showText.equals("Find the green circle.")){
				image.equals(R.drawable.green_oval_button);
		}else{
			if(showText.equals("Find the green rectangle.")){
				image.equals(R.drawable.green_rectangle_button);
		}else{
			if(showText.equals("Find the green square.")){
				image.equals(R.drawable.green_square_button);
		}else{
			if(showText.equals("Find the green triangle.")){
				image.equals(R.drawable.green_triangle_button);
		}else{
			if(showText.equals("Find the orange circle.")){
				image.equals(R.drawable.orange_oval_button);
		}else{
			if(showText.equals("Find the orange square.")){
				image.equals(R.drawable.orange_square_button);
		}else{
			if(showText.equals("Find the orange rectangle.")){
				image.equals(R.drawable.orange_rectangle_button);
		}else{
			if(showText.equals("Find the orange triangle.")){
				image.equals(R.drawable.orange_triangle_button);
		}
		}}}}}}}}}}}}}}}
		return image;
	}*/

}
